#!/bin/bash
##############################################################
# File Name：monitr_zookeeper_kafka.sh
# Version :V1 . 
# Author: wangXiaoFeng 
# Email: 1289832464@qq.com
# Created Time ：2023-02-07 15:43:04
# Description：
#############################################################

# Define variables
# zookeeper binHome directory 
zookeeperBinPath='/opt/app/kafka_2.11-2.1.0/bin'
# zookeeper.properties file path
zookeeperConfPath='/opt/app/kafka_2.11-2.1.0/config/zookeeper.properties'
# monitr_cronTab 
monitr_cronTab='*/1 * * * *'

# Do not change subsequent content!!! 
# Do not change subsequent content!!! 
# Do not change subsequent content!!!
# add crontab
curPath=$(cd `dirname $0`; pwd)
fileName=`echo "$0"|awk -F/ '{print $NF}'`
touch /var/spool/cron/root
sed -i "/${fileName}/d" /var/spool/cron/root
echo "${monitr_cronTab} ${curPath}/${fileName} >> /tmp/${fileName}".log >> /var/spool/cron/root
/sbin/service crond restart $1>/dev/null 2>&1

# get zookeeper info
curDate=`date +"%F %T"`
zookeeperConfFile=`echo ${zookeeperConfPath}|awk -F '/' '{print $NF}'`
zookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "${zookeeperConfFile}"|awk '{print $2}'`

# checking zookeeper
echo -e "\n[Info]:curDate:${curDate}"
if [ -z "${zookeeperPid}" ];then
   echo -e "[Error]:zookeeper is not runing,\n[Info]:Begin start zookeeper......"
   cd ${zookeeperBinPath}
   nohup ./zookeeper-server-start.sh ${zookeeperConfPath} 1>/dev/null 2>&1 &
   sleep 1
   newZookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "zookeeper.properties"|awk '{print $2}'` 
   if [ -z "${newZookeeperPid}" ];then
      echo -e "[Error]:Zookeeper startup failed" 
   else
      echo -e "[Info]:Zookeeper started successfully,Pid is:${newZookeeperPid}"
   fi  
else
  echo -e "[Info]:Zookeeper is runing,Pid is:${zookeeperPid}"
fi
echo -e "\n"